[Node] Undici WebSocket & Diagnostics #621
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Undici WebSocket and Dispatcher / proxy support
This switches the NodeJS WebSocket implementation from the
ws
package toundici
WebSocket.The primary benefit here is that it supports the same Dispatcher interface as for fetch, giving us consistent proxy support. The
proxy-agent
lib used previously had a couple of limitations:The undici WebSocket supports the same
dispatcher
option asfetch
, making the proxy support consistent. I added an example of using this in theexample-node
demo.One major limitation with undici WebSocket is that connection errors show up as just
Received network error or non-101 status code.
, regardless of the cause (connection failure, TLS negotiation failure, proxy failure, etc).The workaround here is to register a custom wrapping Dispatcher that does have access to the underlying error, and emit that on the WebSocket.
Connection diagnostics
Undici has diagnostics_channel support. This gives tracing of network connections, requests and responses. I added an example of using this in
example-node
.This does not log individual messages sent/received over the connection, so this only helps to debug connection-related issues. We can add logging of the websocket messages separately.
Example output with a TLS failure
Error handling
This now passes websocket connection errors through as-is, instead of wrapping them. Wrapping the errors often caused info to get lost, especially in the
JSON.stringify()
part.This now also improves websocket errors for react-native, by using
event.message
(from the example here).